﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace CloudStorageLight.Core.WebDav
{
    public static class AzureUtil
    {
        public static DateTime ConvertFromDateTimeOffset(DateTimeOffset? dateTime)
        {
            if (!dateTime.HasValue) return DateTime.MinValue;
            if (dateTime.Value.Offset.Equals(TimeSpan.Zero))
                return dateTime.Value.UtcDateTime;
            else if (dateTime.Value.Offset.Equals(TimeZoneInfo.Local.GetUtcOffset(dateTime.Value.DateTime)))
                return DateTime.SpecifyKind(dateTime.Value.DateTime, DateTimeKind.Local);
            else
                return dateTime.Value.DateTime;
        }

        public static string GetRelativeRequestPath(string path)
        {
            var result = "";
            bool prefix = false;
            foreach (var item in path.Split('/'))
            {
                if (prefix)
                {
                    result = result + "/" + item;
                }
                else
                {
                    if (string.Equals(item, Settings.WebDavPrefix.Trim('/'), StringComparison.InvariantCultureIgnoreCase))
                    {
                        prefix = true;
                    }
                }
            }
            if (!prefix) throw new ArgumentException(path + " is invalid ", "path");


            return HttpUtility.UrlDecode("/" + result.Trim('/')); ;
        }
    }
}
